#ifndef ITEMDELEGATE_H
#define ITEMDELEGATE_H



#include <QStyledItemDelegate>
#include <QSharedData>
#include <QSharedDataPointer>

class ItemModel;
class QComboBox;
class QAbstractProxyModel;


class ItemDelegate : public QStyledItemDelegate
{
    Q_OBJECT

public:

    enum Filter
    {
        Filter_FIRST = 0,
        IncludeFilter = Filter_FIRST,
        ExcludeFilter,
        Filter_LAST = ExcludeFilter
    };

    ItemDelegate(QObject *parent = 0);

    virtual QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    virtual void setEditorData(QWidget *editor, const QModelIndex &index) const;

    virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;

    /// \brief set filter for a view
    /// \param field is a column index
    /// \param filter is a hit in objects's name
    /// \param filter is inclusive/exclusive
    virtual void setEditorFilter(const int& field, const QString& filterId, const int& filter = IncludeFilter);


protected:

    QWidget* groupEditor(QWidget* parent, const QModelIndex& index) const;
    QWidget* itemEditor(QWidget* parent, const QModelIndex& index) const;
    QWidget* referenceEditor(QWidget* parent, const QModelIndex& index) const;

    void populateCombo(QComboBox* combo, const QModelIndex& index) const;
    void addToCombo(QComboBox* combo, const QModelIndex& optionIndex) const;

    /// \param field is the column of the initial index
    /// \param index is index which is used to add data to some selector
    bool isFiltered(int field, const QModelIndex& index) const;


private:

    struct FilterData
    {
        FilterData()
        {
            field = -1;
            filter = IncludeFilter;
        }

        FilterData(const FilterData& other)
        {
            field = other.field;
            filter = other.filter;
            filterId = other.filterId;
        }

        FilterData operator=(const FilterData& other)
        {
            field = other.field;
            filter = other.filter;
            filterId = other.filterId;

            return *this;
        }

        bool operator==(const FilterData& other)
        {
            return (field == other.field &&
                    filter == other.filter &&
                    filterId == other.filterId);
        }

        bool isValid()
        {
            return (field != -1 && !filterId.isEmpty());
        }

        int field;
        int filter;
        QString filterId;
    };

    QList<FilterData> m_filters;


};



#endif // ITEMDELEGATE_H


